@extends('Admin.layout.layout')

@section('before.css')
    <link href="{{asset('css/plugins/iCheck/custom.css')}}" rel="stylesheet">
@endsection

@section('content')
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>编辑管理员 <small></small></h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t" id="Form" action="/foradmin/adminuser/{{ $admin_user_id }}" method="post">
                            <input type="hidden" name="_method" value="put">
                            <input type="hidden" name="id" value="{{ $admin_user_id }}">
                            {!!  csrf_field() !!}
                            <div class="modal-body">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">用户名称：</label>
                                    <div class="col-sm-8">
                                        <input id="name" name="name" value="{{$name}}" class="form-control" type="text" aria-required="true" class="valid">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">email：</label>
                                    <div class="col-sm-8">
                                        <input id="email" name="email" value="{{$email}}" class="form-control" type="text" aria-required="true" aria-invalid="false" class="valid">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">用户角色：</label>
                                    <input type="hidden" name="operate" >
                                    <div class="col-sm-8">
                                        <div class="radio i-checks">
                                            @if(!empty($rolesAll))
                                                @foreach($rolesAll as $value)
                                                    <label><input type="checkbox" class="record" name="role[{{array_get($value,'role_id')}}]" value="{{array_get($value,'role_id')}}" @if(in_array($value['role_id'],$roleHas)) checked data-type="checked" @else data-type="unchecked" @endif/>{{array_get($value,'display_name')}}</label>
                                                @endforeach
                                            @endif
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group hide">
                                    <label class="col-sm-3 control-label">用户权限：</label>
                                    <div class="col-sm-8">
                                        @if(!empty($permission))
                                            @foreach($permission as $value)
                                                <div class="radio i-checks">
                                                    <label><input type="checkbox" class="checkboxone" name="permission[permission_id][{{array_get($value,'permission_id')}}]"  @if(in_array($value['permission_id'],$permissionHas)) checked @endif value="{{array_get($value,'permission_id')}}"/><font>{{$value['display_name']}}</font></label>
                                                    @if(!empty($value['group']))
                                                        @foreach($value['group'] as $item)
                                                            <label><input type="checkbox" class="checkboxtwo" name="permission[{{array_get($item,'id')}}]" value="{{array_get($item,'id')}}"/>{{array_get($item,'name')}}</label>
                                                        @endforeach
                                                    @endif
                                                </div>
                                            @endforeach
                                        @endif
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <a href="/foradmin/adminuser" class="btn btn-default pull-left">返回</a>
                                <button type="submit" class="btn btn-primary" id="btn_submit">
                                    确定
                                </button>
                                <button type="button" class="btn btn-default"
                                        data-dismiss="modal" >取消
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('js/plugins/iCheck/icheck.min.js')}}"></script>
    <script src="{{asset('js/plugins/validate/jquery.validate.min.js')}}"></script>
    <script src="{{asset('js/plugins/validate/messages_zh.min.js')}}"></script>
    <script src="{{asset('js/plugins/layer/layer.min.js')}}"></script>
    <script src="{{asset('js/plugins/chosen/chosen.jquery.js')}}"></script>
    <script>
        $.validator.setDefaults({
            highlight: function (element) {
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
            },
            success: function (element) {
                element.closest('.form-group').removeClass('has-error').addClass('has-success');
            },
            errorElement: "span",
            errorPlacement: function (error, element) {
                if (element.is(":radio") || element.is(":checkbox")) {
                    error.appendTo(element.parent().parent().parent());
                } else {
                    error.appendTo(element.parent());
                }
            },
            errorClass: "help-block m-b-none",
            validClass: "help-block m-b-none"


        });
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
            $('.checkboxone').on('ifChecked', function(event){
                var _this = this;
                getcheckedone(_this);
            })
            $('.checkboxone').on('ifUnchecked', function(event){
                var _this = this;
                getcheckedone(_this);
            })
            $('.checkboxtwo').on('ifChanged',function(event){
                var _this = this;
                if($(_this).parent().parent().parent().find('input[type = "checkbox"][class="checkboxtwo"]').filter(':checked').length == $(_this).parent().parent().parent().find('input[type = "checkbox"][class="checkboxtwo"]').length){
                    $(_this).parent().parent().parent().find('input[type = "checkbox"][class="checkboxone"]').prop('checked',true);
                }else{
                    $(_this).parent().parent().parent().find('input[type = "checkbox"][class="checkboxone"]').prop('checked',false);
                }
                $(_this).parent().parent().parent().find('input[type = "checkbox"][class="checkboxone"]').iCheck('update');
            })
        });
        function getcheckedone(_this){
            if($(_this).is(':checked')){
                $(_this).parent().parent().parent().find('input[type = "checkbox"]').each(function(){
                    $(this).iCheck('check');
                });
            }else{
                $(_this).parent().parent().parent().find('input[type = "checkbox"]').each(function(){
                    $(this).iCheck('uncheck');
                });
            }
        }
        var operate = new Array();
        // 记录页面操作，确定到PHP是否增删。
        $('.record').on('ifChecked', function(event){
            var _this = this;
            var splice = _this.value+'-add';
            var type = $(_this).data('type');
            // 原有的被 选中 ，删除数组里存的。
            if(type=='checked'){
                var delStr = _this.value+'-delete';
                operate.splice($.inArray(delStr,operate),1);
            // 原来没有的被 选中 存储。
            }else{
                operate.push(splice);
            }
            // 赋值给指定的 隐藏域
            $('input[name=operate]').val(operate);
        });
        $('.record').on('ifUnchecked', function(event){
            var _this = this;
            var splice = _this.value+'-delete';
            var type = $(_this).data('type');
            // 原有的被取消
            if(type=='checked'){
                operate.push(splice);
                // 不是数字取消 后台不做操作
            // 原来没有的被 取消
            }else{
                var delStr = _this.value+'-add';
                operate.splice($.inArray(delStr,operate),1);
            }
            // 赋值给指定的 隐藏域
            $('input[name=operate]').val(operate);

        });

        // validate signup form on keyup and submit
        var icon = "<i class='fa fa-times-circle'></i> ";
        // 修改
        $("#Form").validate({
            rules: {
                name: {
                    required:true,
                    minlength:4,
                },
                email: {
                    required:true,
                    email:true,
                },
                role: "required",
            },
            messages: {
                name: {
                    required:icon + "请输入用户名称",
                    minlength:icon + "用户名最少输入四个字符",
                },
                email: {
                    required:icon + "邮箱地址",
                    email:icon + "邮箱格式不正确",
                },
                role: icon + "请选择用户角色",
            },
            submitHandler:function(form){
                var name = $('#name').val();
                var email = $('#email').val();
                var role = $("input[name^=role]:checked").length;
                if(role==0){
                    layer.alert("请选择用户角色", {icon: 2});
                }else{
                    var data = $("#Form").serialize();
                    console.log(data);
                    $.ajax({
                        url : '/foradmin/adminuser/{{ $admin_user_id }}',
                        type : 'post',
                        dataType : "json",
                        data : data,
                        success : function(data) {
                            if (0 == data.code) {
                                layer.alert('保存成功', {
                                    icon: 1
                                }, function () {
                                    location.href = "/foradmin/adminuser";
                                });
                            } else {
                                layer.alert(data.message, {
                                    icon: 2
                                });
                            }
                        },
                    });

                }
            }
        });

    </script>
@endsection
